using System;
using System.Collections;
using Engine;
using Game;

namespace RuthlessConquest {
    public class ListSelectionDialog : Dialog {
        public LabelWidget TitleLabelWidget;

        public CanvasWidget ContentWidget;

        public double? DismissTime;

        public bool IsDismissed;

        public Action<object> SelectionHandler;

        public ListPanelWidget ListWidget { get; }

        public Color SelectionColor {
            get => ListWidget.SelectionColor;
            set => ListWidget.SelectionColor = value;
        }

        public LayoutDirection Direction {
            get => ListWidget.Direction;
            set {
                if (value != ListWidget.Direction) {
                    ListWidget.Direction = value;
                    OptimizeSize();
                }
            }
        }

        public Vector2 ContentSize {
            get => ContentWidget.Size;
            set {
                if (value != ContentWidget.Size) {
                    ContentWidget.Size = value;
                    OptimizeSize();
                }
            }
        }

        public ListSelectionDialog(string title,
            IEnumerable items,
            float itemSize,
            Func<object, Widget> itemWidgetFactory,
            Action<object> selectionHandler) {
            StackPanelWidget root = new() {
                Direction = LayoutDirection.Vertical, HorizontalAlignment = WidgetAlignment.Center, VerticalAlignment = WidgetAlignment.Center
            };
            AddChildren(root);
            TitleLabelWidget = new LabelWidget {
                Name = "ListSelectionDialog.Title",
                Color = Colors.Fore,
                FontScale = 0.75f,
                DropShadow = true,
                Margin = new Vector2(0f, 8f),
                HorizontalAlignment = WidgetAlignment.Center
            };
            root.AddChildren(TitleLabelWidget);
            ContentWidget = new CanvasWidget { Name = "ListSelectionDialog.Content", Size = new Vector2(600f, 400f), ClampToBounds = true };
            root.AddChildren(ContentWidget);
            ContentWidget.AddChildren(new RectangleWidget { FillColor = Colors.Back, OutlineColor = Colors.ForeDim, OutlineThickness = 2f });
            ContentWidget.AddChildren(new InterlaceWidget());
            ListWidget = new ListPanelWidget {
                Name = "ListSelectionDialog.List",
                Direction = LayoutDirection.Vertical,
                Margin = new Vector2(3, 3),
                HorizontalAlignment = WidgetAlignment.Center,
                VerticalAlignment = WidgetAlignment.Center
            };
            ContentWidget.AddChildren(ListWidget);
            SelectionHandler = selectionHandler;
            TitleLabelWidget.Text = title;
            TitleLabelWidget.IsVisible = !string.IsNullOrEmpty(title);
            ListWidget.ItemSize = itemSize;
            if (itemWidgetFactory != null) {
                ListWidget.ItemWidgetFactory = itemWidgetFactory;
            }
            foreach (object item in items) {
                ListWidget.AddItem(item);
            }
            OptimizeSize();
            ListWidget.ScrollPosition = 0f;
        }

        public ListSelectionDialog(string title,
            IEnumerable items,
            float itemSize,
            Func<object, string> itemToStringConverter,
            Action<object> selectionHandler) : this(
            title,
            items,
            itemSize,
            item => new LabelWidget {
                Text = itemToStringConverter(item), HorizontalAlignment = WidgetAlignment.Center, VerticalAlignment = WidgetAlignment.Center
            },
            selectionHandler
        ) { }

        public void OptimizeSize() {
            for (int num = ListWidget.Items.Count; num >= 0; num--) {
                float num2 = MathUtils.Min(num + 0.5f, ListWidget.Items.Count);
                if (Direction == LayoutDirection.Vertical) {
                    if (num2 * ListWidget.ItemSize <= ContentWidget.Size.Y) {
                        ContentWidget.Size = new Vector2(ContentWidget.Size.X, num2 * ListWidget.ItemSize);
                        break;
                    }
                }
                else if (num2 * ListWidget.ItemSize <= ContentWidget.Size.X) {
                    ContentWidget.Size = new Vector2(num2 * ListWidget.ItemSize, ContentWidget.Size.Y);
                    break;
                }
            }
        }

        public override void Update() {
            if (Input.Back
                || Input.Cancel
                || (Input.Tap.HasValue && !ListWidget.HitTest(Input.Tap.Value))) {
                DismissTime = 0.0;
            }
            else if (!DismissTime.HasValue
                && ListWidget.SelectedItem != null) {
                DismissTime = Time.FrameStartTime + 0.05000000074505806;
            }
            if (DismissTime.HasValue
                && Time.FrameStartTime >= DismissTime.Value) {
                Dismiss(ListWidget.SelectedItem);
            }
        }

        public void Dismiss(object result) {
            if (!IsDismissed) {
                IsDismissed = true;
                DialogsManager.HideDialog(this);
                if (SelectionHandler != null
                    && result != null) {
                    SelectionHandler(result);
                }
            }
        }
    }
}